﻿using System;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using FUSE.AzureConfig.Tasks;
using System.Collections.Generic;

namespace AzureConfGen.Tests
{
    [TestClass]
    public class BasicReadingTests
    {
        [TestMethod]
        public void EmptyDoc_NoEnvs()
        {
            var confStr = "{}";
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            Assert.IsNotNull(conf.Environments);
            Assert.IsFalse(conf.Environments.Any());
        }

        [TestMethod]
        public void NoEnvsDoc_NoEnvs()
        {
            var confStr = Properties.Resources.EmptyConf;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            Assert.IsNotNull(conf.Environments);
            Assert.IsFalse(conf.Environments.Any());
        }

        [TestMethod]
        public void BasicDoc_SingleEnvSingleRole()
        {
            var confStr = Properties.Resources.BasicValidConf;
            var conf = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            Assert.IsNotNull(conf.Environments);
            EnvironmentSpec env;
            Assert.IsTrue(conf.Environments.TryGetValue("basic", out env));
            Assert.AreEqual("Foo", env.ProfileName);
            Assert.IsNotNull(env.Roles);
            Assert.AreEqual(1, env.Roles.Count);
            RoleSpec role;
            Assert.IsTrue(env.Roles.TryGetValue("WebRole", out role));
            Assert.AreEqual(2, role.Instances);
            Assert.IsNotNull(env.ConfigSettings);
            Dictionary<string, string> settings;
            Assert.IsTrue(env.ConfigSettings.TryGetValue("WebRole", out settings));
            Assert.AreEqual("val1", settings["key1"]);
            Dictionary<string, CertSpec> certs;
            Assert.IsTrue(env.Certificates.TryGetValue("WebRole", out certs));
            CertSpec cert;
            Assert.IsTrue(certs.TryGetValue("Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption", out cert));
            Assert.AreEqual("fakeguidvalue", cert.Thumbprint);
            Assert.AreEqual("sha1", cert.ThumbprintAlgorithm);
            Assert.IsTrue(certs.TryGetValue("Cert2", out cert));
            Assert.AreEqual("sha1", cert.ThumbprintAlgorithm, "Default not set correctly");
        }

        [TestMethod]
        public void BasicDoc_MultiRoleFallthrough()
        {
            var confStr = Properties.Resources.MultiRoleFallthrough;
            var spec = JsonConvert.DeserializeObject<AzureConfigSpec>(confStr);
            Assert.IsNotNull(spec.Environments);
            var processor = new SpecProcessor();
            var conf = processor.Process(spec);
            Assert.IsTrue(conf.Environments.ContainsKey("Foo"));
            var env = conf.Environments["Foo"];
            Assert.AreEqual(2, env.Roles.Count);
            Assert.IsTrue(env.Roles.ContainsKey("Web") && env.Roles.ContainsKey("Worker"));
            var web = env.Roles["Web"];
            Assert.AreEqual("base1", web.ConfigSettings.Settings["key1"]);
            Assert.AreEqual("web2", web.ConfigSettings.Settings["key2"]);
            var worker = env.Roles["Worker"];
            Assert.AreEqual("worker1", worker.ConfigSettings.Settings["key1"]);
            Assert.AreEqual("base2", worker.ConfigSettings.Settings["key2"]);
            Assert.AreEqual(2, web.Certificates.Count);
            Assert.AreEqual("fakeguidvalue", web.Certificates["Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption"].Thumbprint);
            Assert.AreEqual("fakeguidvalue2", web.Certificates["Cert2"].Thumbprint);
            Assert.AreEqual("sha1", web.Certificates["Cert2"].ThumbprintAlgorithm);
            Assert.AreEqual(2, worker.Certificates.Count);
            Assert.AreEqual("fakeguidvalue", worker.Certificates["Microsoft.WindowsAzure.Plugins.RemoteAccess.PasswordEncryption"].Thumbprint);
            Assert.AreEqual("fakeguidvalue2_worker", worker.Certificates["Cert2"].Thumbprint);
            Assert.AreEqual("sha1", web.Certificates["Cert2"].ThumbprintAlgorithm);
        }
    }
}
